package com.tao.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.tao.config.MyLog;
import com.tao.pojo.Category;
import com.tao.pojo.vo.ResponseResult;
import com.tao.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/category")
public class CmsCategoryController {
    @Autowired
    private CategoryService categoryService;

    @MyLog("分类列表")
    @PreAuthorize("hasAnyAuthority('admin')")
    @PostMapping("/list")
    public Object list(@RequestBody Category category){
        LambdaQueryWrapper<Category> lqw = new LambdaQueryWrapper<>();
        lqw.like(!ObjectUtils.isEmpty(category.getName()),Category::getName,category.getName());
        List<Category> list = categoryService.list(lqw);
        return ResponseResult.success(list);
    }

    @MyLog("分类新增")
    @PreAuthorize("hasAnyAuthority('admin')")
    @PostMapping("/add")
    public Object add(@RequestBody Category category){
        categoryService.save(category);
        return ResponseResult.success(null);
    }

    @MyLog("分类修改")
    @PreAuthorize("hasAnyAuthority('admin')")
    @PostMapping("/update")
    public Object update(@RequestBody Category category){
        categoryService.updateById(category);
        return ResponseResult.success(null);
    }

    @MyLog("分类删除")
    @PreAuthorize("hasAnyAuthority('admin')")
    @PostMapping("/remove")
    public Object remove(@RequestBody Category category){
        categoryService.removeById(category.getId());
        return ResponseResult.success(null);
    }
}
